<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- 渲染表单 -->
      <el-table :data="CateList" style="width: 100%" border stripe>
        <el-table-column type="index" label="序号" width="50" align="center">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="alterCate(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="removeCate(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加文章分类的dialog -->
    <el-dialog title="添加文章分类" :visible.sync="addVisible" width="35%" @closed="$refs.addForm.resetFields()" :close-on-click-modal="false">
      <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="70px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="hSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改文章分类的dialog -->
    <el-dialog title="添加文章分类" :visible.sync="editVisible" width="35%" @closed="$refs.editForm.resetFields()" :close-on-click-modal="false">
      <el-form :model="editForm" :rules="addRules" ref="editForm" label-width="70px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="hEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      CateList: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取文章分类
    async loadCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      // 将拿到的数据存到 data 里面
      this.CateList = res.data
    },
    // 添加文章分类
    hSubmit () {
      // 1、兜底校验
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        // 2、校验通过发起请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        // 3、根据请求结果提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4、关闭 dialog
        this.addVisible = false
        // 5、重新获取文章分类渲染页面
        this.loadCateList()
      })
    },
    // 点击修改文章分类弹出 dialog
    alterCate (row) {
      // 1、管理员不允许修改 id 为 1/2 的文章分类
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改该分类！')
      // 2、深拷贝每一行分类的数据
      this.editForm = { ...row }
      // 3、dialog 显示
      this.editVisible = true
    },
    // 点击确定修改文章分类
    hEdit () {
      // 1、兜底校验
      this.$refs.editForm.validate(async valid => {
        // 趁早返回
        if (!valid) return
        // 2、验证通过发送请求修改文章分类
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // 3、根据请求结果，通知用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4、关闭 dialog
        this.editVisible = false
        // 5、重新发起请求渲染文章分类
        this.loadCateList()
      })
    },
    // 删除文章分类
    async removeCate (row) {
      // 1、管理员不允许删除 id 为 1/2 的文章分类
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许删除该分类！')
      const result = await this.$confirm('此操作将永久删除该分类,是否继续？', '提示', { type: 'warning' }).catch(err => err)
      if (result !== 'confirm') return
      // 2、发请求删除文章分类
      // 发请求 delete get 这两个 api 第二步参数都是 config 配置对象
      // put post patch 这三个 api 第二个参数都是 data
      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id: row.id } })
      // 3、根据请求结果提醒用户
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      // 4、重新发起请求渲染文章分类
      this.loadCateList()
    }
  },
  created () {
    this.loadCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
